package com.tianyalei.wolf.mddesignlib.view.editinputview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.design.widget.TextInputLayout;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.View;

import com.tianyalei.wolf.mddesignlib.R;

/**
 * Created by Bo on 2016/3/7.
 */
public class EditInputView extends TextInputLayout {
    private DeletableEditText mEdit;

    public EditInputView(Context context) {
        super(context, null);
    }

    public EditInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mEdit = new DeletableEditText(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.EditInputView);
        String hint = ta.getString(R.styleable.EditInputView_hint);
        boolean isNum = ta.getBoolean(R.styleable.EditInputView_isNum, false);
        boolean isPassword = ta.getBoolean(R.styleable.EditInputView_isPassword, false);
        int id = ta.getResourceId(R.styleable.EditInputView_bg, -1);
        int size = ta.getDimensionPixelSize(R.styleable.EditInputView_size, 18);
        int color = ta.getColor(R.styleable.EditInputView_textcolor, Color.GRAY);
        mEdit.setSingleLine(true);
        mEdit.setHint(hint);
        mEdit.setTextSize(size);
        mEdit.setTextColor(color);
        if (isNum) {
            //设置EditText输入类型为数字
            mEdit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
        } else {
            //设置EditText输入类型为文本
            mEdit.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_NUMBER_VARIATION_NORMAL);
        }
        if (!isPassword) {
            //设置EditText文本为可见的
            mEdit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            //设置EditText文本为隐藏的
            mEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        if (id != -1) {
            mEdit.setBackgroundResource(id);
        }
        //当输入框获得焦点的时候 隐藏错误消息
        mEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    EditInputView.this.setErrorEnabled(false);
                }
            }
        });
        this.addView(mEdit);

    }

    /**
     * 显示错误信息
     * @param msg
     */
    public void showErrorMsg( String msg) {
        EditInputView.this.setErrorEnabled(true);
        EditInputView.this.setError(msg);
    }

    /**
     * 是否显示清除右边按钮
     *
     * @drawableId 图片id
     */
    public void setRightIconShow(int drawableId) {
        mEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableId, 0);
        mEdit.setPadding(0, 0, 1, 0);
    }
}
